package com.buss.admin.controller;

import com.buss.admin.trans.form.VipUserForm;
import com.buss.admin.trans.form.VipUserVo;
import com.buss.common.entity.HxVipUserEntity;
import com.buss.common.model.json.EasyUIDataGridResult;
import com.buss.common.model.json.HXResult;
import com.buss.common.service.SysServiceI;
import com.buss.common.util.IDUtils;
import org.jweb.core.bean.PageMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.jweb.core.util.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Administrator on 2016/5/5.
 */
@Controller
@RequestMapping("/admin/vipuser")
public class VipUserAdminController {

    @Autowired
    private SysServiceI sysService;

    @RequestMapping("/list")
    @ResponseBody
    public EasyUIDataGridResult getMemberList(VipUserForm searchForm, HttpServletRequest request) {
        // 分页查询
        searchForm.setIsDelete("0");
        CriteriaQuery cq = searchForm.getCriteriaQuery();
        int pageNo = Integer.parseInt(request.getParameter("page").toString());
        int pageSize = Integer.parseInt(request.getParameter("rows").toString());
        Map< String, Object> orderby = new HashMap<>();
        orderby.put("createtime", "desc");
        cq.setOrder(orderby);
        // 定义返回数据格式
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        PageMode<HxVipUserEntity> pageMode = sysService.list4page(cq, pageNo, pageSize);

        List<HxVipUserEntity> uas = pageMode.getResult();
        List<VipUserVo> vipUserVos = new ArrayList<>();
        VipUserVo vipUserVo = null;
        for(HxVipUserEntity ua : uas) {
            vipUserVo = new VipUserVo();
            vipUserVo.copyEntity(ua);
            vipUserVos.add(vipUserVo);
        }
        // 封装数据
        result.setRows(vipUserVos);
        result.setTotal(pageMode.getTotal());
        return result;
    }

    @RequestMapping("/save")
    @ResponseBody
    public HXResult articleSave(VipUserForm form, String content1, String content2) {
        try {
            HxVipUserEntity entity = new HxVipUserEntity();
            MyBeanUtils.copyBeanNotNull2Bean(form, entity);
			/*
			 * 插入数据操作
			 */
            entity.setHeadImg(form.getImgUrl());
            entity.setPersionsummary(content1);
            entity.setCompanysummary(content2);

            entity.setId(IDUtils.getArticleId()+"");
            // 补全日期属性值
            Date date = new Date();
            entity.setUpdatetime(date);
            entity.setCreatetime(date);

            entity.setIsDelete(0);
            entity.setLevelclass(0);
            entity.setSort(0);
//            this.sysService.saveOrUpdate(entity);
//            this.sysService.save(entity);
            String sql = "INSERT INTO `hxclub`.`hx_VipUser` "
                    + "(`id`, `name`, `head_img`, `job`, `levelclass`, `levelname`, `persionsummary`, `companysummary`, `createTime`, `updateTime`, `sort`, `is_delete`)  "
                    + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            this.sysService.executeSql(sql,
                    entity.getId(),
                    entity.getName(),
                    entity.getHeadImg(),
                    entity.getJob(),
                    entity.getLevelclass(),
                    entity.getLevelname(),
                    entity.getPersionsummary(),
                    entity.getCompanysummary(),
                    entity.getCreatetime(),
                    entity.getUpdatetime(),
                    entity.getSort(),
                    entity.getIsDelete());

            return HXResult.ok("发布成功！");

        } catch (Exception e) {
            e.printStackTrace();
            return HXResult.build(500, "服务器繁忙，发布文章失败！请稍后再试！");
        }
    }

    @RequestMapping(value="/addorupdate")
    @ResponseBody
    public HXResult getAdd(HxVipUserEntity vipUserEntity ) {
        try {

            Date datatime  = new Date();
            vipUserEntity.setCreatetime(datatime);
            vipUserEntity.setUpdatetime(datatime);
            vipUserEntity.setIsDelete(0);

            sysService.saveOrUpdate(vipUserEntity);
            return HXResult.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return HXResult.build(500, "添加失败");
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public HXResult delete(String ids, HttpServletRequest request) {
        try {
            String[] idStrings = ids.split(",");
            for (String id : idStrings) {
                HxVipUserEntity vipUserEntity = sysService.get(HxVipUserEntity.class, id);
                vipUserEntity.setIsDelete(1);
                vipUserEntity.setUpdatetime(new Date());
                sysService.saveOrUpdate(vipUserEntity);
            }

            return HXResult.ok("删除活动成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return HXResult.build(500, "服务器发生错误，删除活动数据失败！");
        }
    }

    @RequestMapping(value = "/update", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public HXResult update(VipUserForm form, String content1, String content2) {
        try {
            HxVipUserEntity entity = new HxVipUserEntity();
            MyBeanUtils.copyBeanNotNull2Bean(form, entity);
			/*
			 * 插入数据操作
			 */
            entity.setHeadImg(form.getImgUrl());
//            entity.setHeadImg(form.getHeadImg());
            entity.setPersionsummary(content1);
            entity.setCompanysummary(content2);
            entity.setUpdatetime(new Date());

            String sql = "UPDATE hx_VipUser set name =?,head_img = ?,job = ?,levelclass = ?,levelname = ?,persionsummary = ?,companysummary = ?,createTime = ?,updateTime = ?,sort = ? " +
                    " WHERE id = ?";
            this.sysService.executeSql(sql,
                    entity.getName(),
                    entity.getHeadImg(),
                    entity.getJob(),
                    entity.getLevelclass(),
                    entity.getLevelname(),
                    entity.getPersionsummary(),
                    entity.getCompanysummary(),
                    entity.getCreatetime(),
                    entity.getUpdatetime(),
                    entity.getSort(),
                    entity.getId());

            return HXResult.ok("修改成功");
        } catch (Exception e) {

            e.printStackTrace();
            return HXResult.build(500, "修改失败");
        }
    }



    @RequestMapping("/{path}")
    public String goPath(@PathVariable String path) {
        System.out.println("path:" + path);
        return "admin/vipuser/" + path;
    }

}
